package com.zhijiaoyun.common;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author shisfish
 * @date 2017/6/6
 */
public class StringUtils {

	private static final String COMMA = ",";

	public static boolean isContain(String[] args, String target) {
		if (args != null && args.length > 0) {
			for (String string : args) {
				if (string.equals(target)) {
					return true;
				}
			}
		}
		return false;
	}

	public static boolean isContain(String arg, String target) {
		if (arg != null) {
			return isContain(arg.split(COMMA), target);
		}
		return false;
	}

	public static boolean isContain(String[] args, String[] targets) {
		return false;
	}

	public static boolean isBlank(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if ((Character.isWhitespace(str.charAt(i)) == false)) {
				return false;
			}
		}
		return true;
	}
	
	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}

	/**
	 * 判断String是否为空
	 *
	 */
	public static boolean isEmptyString(String string) {
		if (string == null || string.isEmpty()) {
			return true;
		}
		return false;
	}

	public static boolean isNotEmptyString(String string) {
		return !isEmptyString(string);
	}

	public static boolean isNullString(String string) {
		if (string == null || string.isEmpty() || Constant.NULL.equals(string)) {
			return true;
		}
		return false;
	}

	public static boolean isNotNullString(String string) {
		return !isNullString(string);
	}

	/**
	 *
	 * @Description 判断对象是否为空
	 * @param object
	 * @return
	 */
	public static boolean isEmptyObject(Object object) {
		if (object == null || object.toString().isEmpty()) {
			return true;
		}
		return false;
	}

	/**
	 *
	 * @Description 判断list集合是否为空
	 * @param list
	 * @return
	 */
	public static boolean isEmptyList(List<?> list) {
		if (list == null || list.size() == 0) {
			return true;
		}
		return false;
	}

	/**
	 * 判断list集合是否为空
	 * @param list
	 * @return
	 */
	public static boolean isNotEmptyList(List<?> list) {
		return !isEmptyList(list);
	}

	/**
	 *
	 * @Description 判断map是否为空
	 * @param map
	 * @return
	 */
	public static boolean isEmptyMap(Map<?, ?> map) {
		if (map == null || map.size() == 0) {
			return true;
		}
		return false;
	}

	/**
	 * String -> int
	 *
	 * @param string
	 * @return int
	 */
	public static int parseInt(String string, int def) {
		if (isEmptyString(string)) {
			return def;
		}
		int num = def;
		try {
			num = Integer.parseInt(string);
		} catch (Exception e) {
			num = def;
		}
		return num;
	}

	/**
	 * String -> long
	 *
	 * @param string
	 * @return long
	 */
	public static long parseLong(String string, long def) {
		if (isEmptyString(string)) {
			return def;
		}
		long num;
		try {
			num = Long.parseLong(string);
		} catch (Exception e) {
			num = def;
		}
		return num;
	}

	/**
	 * String -> double
	 *
	 * @param string
	 * @return long
	 */
	public static double parseDouble(String string, double def) {
		if (isEmptyString(string)) {
			return def;
		}
		double num;
		try {
			num = Double.parseDouble(string);
		} catch (Exception e) {
			num = def;
		}
		return num;
	}

	/**
	 * String -> float
	 *
	 * @param string
	 * @return long
	 */
	public static float parseFloat(String string, float def) {
		if (isEmptyString(string)) {
			return def;
		}
		float num;
		try {
			num = Float.parseFloat(string);
		} catch (Exception e) {
			num = def;
		}
		return num;
	}

	/**
	 * String -> float
	 *
	 * @param string
	 * @return long
	 */
	public static float parseFloat(String string, float def, int digit) {
		if (isEmptyString(string)) {
			return def;
		}
		float num;
		try {
			num = Float.parseFloat(string);
		} catch (Exception e) {
			num = def;
		}
		if (digit > 0 && num != def) {
			BigDecimal bigDecimal = new BigDecimal(num);
			float twoDecimalP = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
			return twoDecimalP;
		} else {
			return num;
		}
	}

}
